package com.kanshu.ksgb.fastread.doudou.module.bookcity.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dl7.recycler.helper.RecyclerViewHelper;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseActivity;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.view.EmptyLayout;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.adapter.SelectedBottomAdapter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.BookSet;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter.BookCityPresenter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.retrofit.BookSetRequestParams;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.utils.BookCityUtils;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wcy on 18/5/25.
 */
@Route(path = ARouterConfig.BOOK_MENU_DETAIL)
public class BookMenuListActivity extends BaseActivity {
    public static final String EXTRA_TITLE = "title";
    public static final String EXTRA_ID = "id";
    @BindView(R.id.recyler_view)
    RecyclerView mRecylerView;
    @BindView(R.id.empty_layout)
    EmptyLayout mEmptyLayout;
    @BindView(R.id.swipe_refresh)
    TwinklingRefreshLayout mSwipeRefresh;
    private String mTitle;
    private String mMenuId;
    private TextView mDesc;
    private RoundedImageView mRoundedImageView;
    private BookCityPresenter mPresenter;
    private SelectedBottomAdapter mAdapter;
    private List<BookInfo> mBookInfos = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_common_refresh_rv);
        ButterKnife.bind(this);
        parseIntent();
        setTitle(mTitle);
        init();
    }

    private void init() {
        mPresenter = new BookCityPresenter(lifeCyclerSubject);
        mSwipeRefresh.setPureScrollModeOn();
        mAdapter = new SelectedBottomAdapter(this, mBookInfos);
        View view = LayoutInflater.from(this).inflate(R.layout.item_header_book_menu_layout, null);
        mDesc = (TextView) view.findViewById(R.id.desc);
        mRoundedImageView = (RoundedImageView) view.findViewById(R.id.cover);
        mAdapter.addHeaderView(view);
        mAdapter.addFooterView(BookCityUtils.addEmptyFooter(this, 0, 0));
        RecyclerViewHelper.initRecyclerViewV(this, mRecylerView, mAdapter);
        BookSetRequestParams params = new BookSetRequestParams();
        params.id = mMenuId;
        mPresenter.getBookSet(params, new INetCommCallback<BookSet>() {
            @Override
            public void onResponse(BookSet bookSet) {
                if (mEmptyLayout != null) {
                    mEmptyLayout.hide();
                }
                mDesc.setText(bookSet.title_desc);
                GlideImageLoader.load(bookSet.title_big_img, mRoundedImageView, GlideImageLoader.getEmptyConfig());
                mBookInfos.addAll(bookSet.book_ids);
                mAdapter.notifyDataSetChanged();
            }

            public void showEmptyByCode(int code) {
                int status = EmptyLayout.STATUS_NO_NET;
                if (code == Constants.ErrCode.NoData) {
                    status = EmptyLayout.STATUS_NO_DATA;
                }
                if (mEmptyLayout != null) {
                    mEmptyLayout.setEmptyStatus(status);
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                showEmptyByCode(code);
            }
        });
        mAdapter.setOnItemClickListener((view1, position) -> {
            position = position - mAdapter.getHeaderViewsCount();
            ARouterUtils.toActivity(ARouterConfig.BOOK_DETAIL, "book_id", mBookInfos.get(position).book_id);
        });
    }

    private void parseIntent() {
        mTitle = getIntent().getStringExtra(EXTRA_TITLE);
        mMenuId = getIntent().getStringExtra(EXTRA_ID);
    }
    @Override
    public void finish() {
        Utils.jumpToHomeIfNeed();
        super.finish();
    }

}
